<?php
session_start ();
require_once './php_lib/conexion.php';

if(isset($_POST['lista2'])){
	
	$lista_e= unserialize(stripslashes($_POST['lista2']));
	
	$fecha = "SELECT NOW() AS F ";
	
	$id_venta = "SELECT MAX( ven_id )+1 AS Id FROM Venta";
	
	$qiv = mysql_query($id_venta,$db_zapatos) or die(mysql_error());
	$qiv_r= mysql_fetch_assoc($qiv);  
	
	$venta = "INSERT INTO `Venta` (
			`ven_id` ,
			`ven_fec` ,
			`Per_rut`
			)
			SELECT '".$qiv_r['Id']."',F, '".$_POST['rut']."' FROM 
			(".$fecha.") AS T";
	
	$qv = mysql_query($venta, $db_zapatos)or die(mysql_error());
	
	foreach($lista_e as $lista){
		
	$sector = "SELECT `sec_id` FROM `Sector`
				WHERE `bod_id` IN (SELECT `bod_id` FROM Bodega
				WHERE suc_id = (SELECT suc_id FROM Personal
				WHERE per_rut = ".$_POST['rut']."))";
	
	$actualizar = "UPDATE Pro_Sec 
				SET pros_stock =  (pros_stock-".$lista['cant'].")
				WHERE prod_id=".$lista['prod_id']."
				AND sec_id in (".$sector.") "; 
	
	$qa = mysql_query($actualizar, $db_zapatos)or die(mysql_error());
	
	$insertar = "INSERT INTO Prod_Ven (
				`prod_id`,
				`ven_id`,
				`prodv_can`,
				`prodv_valor`)
				VALUES (
				'".$lista['prod_id']."',
				'".$qiv_r['Id']."',
				'".$lista['cant']."',
				'".$lista['subt']."')";
	$qi = mysql_query($insertar, $db_zapatos)or die(mysql_error());
	}

	echo "<a href=\"#\"onclick=\"$('#mainDiv').load('./realizar_venta.php');\">Venta Realizada</a>";
}
?>
